#include <iostream>
#include <unordered_set>
using namespace std;

class Solution {
public:
    int getSum(int n){
        int sum = 0;
        while(n){
            sum += (n % 10) * (n % 10);
            n /= 10;
        }
        return sum;
    }
    bool isHappy(int n) {
        unordered_set<int> ss;
        while(true){
            int sum = getSum(n);
            if(sum == 1){
                return true;
            }
            if(ss.find(sum) != ss.end()){
                return false;
            }else{
                ss.insert(sum);
            }
            n = sum;
        }
    }
};
